<?php
/**
 *       _____
 *      /  \  \      _       _    _____________   ___     ___ '  TM
 *     / /\ \  \    | |\    | |\ /_____   _____\ | - |   | - |
 *    / /  \ \  \   | | |   | | ||____/| |\____| | - |___| - |
 *   / /   /\ \  \  | | |   | | |    | | | |     | -  ___  - |
 *  / /   /  \ \  \ | |_|___| | |    | |_| |     | - |   | - |
 * / /___/    \ \__\|________ |/     |/___\|     |___|   |___|
 *________________________________________________________________________
 *
 * Class Config permettant un acès en lecture/ecriture à la configuration du CMS
 *
 *
 * PHP version 5
 *
 * LICENSE: GNU General Public License v3
 *
 * @package    config
 * @subpackage core
 * @author     Poly Charly <CharlyPoly@gmail.fr>
 * @copyright  2009 Authentic Development Group
 * @license    GNU General Public License v3
 *
 **/
class config{
    private $configs = NULL;
    private $files   = array('php'=>'Config_PHP.php', 'dataServer'=>'Config_Database.php','site'=>'Config_Site.php');

    public function __construct() {
        if (!in_array( 'config',get_declared_classes())) {
            $this->load();
        } else {
            Error::getInstance(E_ERROR ,'Config is a Singleton, cannot redeclare !', __FILE__, __LINE__);
        }
    }

    public function set($type, $name, $newVal) {
        //@todo  write in php file
    }

    private function load() {
        foreach ($this->files as $f=>$file) {
            include($file);
            $this->{$f} = new configList($name, new $f);
        }
    }

    static private function  objToArr($obj) {
        $arr = array();
        foreach ($obj as $k=>$v) {
            $arr[$k] = $v;
        }
        return $arr;
    }
    
}

class configList  extends config implements arrayaccess {

    private $tab  = array();
    private $name = NULL;

    public function __construct($name, $obj) {
        $this->name = $name;
        $this->tab  = parent::objToArr($obj);
    }

    public function offsetSet($o, $value) {
        $this->set($this->name, $o, $value);
    }
    public function offsetExists($o) {
        return isset($this->tab[$o]);
    }
    public function offsetUnset($o) {
        return false;
    }
    public function offsetGet($o) {
        return isset($this->tab[$o]) ? $this->tab[$o] : null;
    }

}
/*
 * @example
 * $config = new config();
 * $config->site['Title'] = "Salut les amis !";
 * echo $config->site['Title'];
 * // -> Salut les amis !
 * // Le <title> du site est maintenant "Salut les amis !"
 */
?>
